package com.example.courseselectionsystemconsume.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @description: TODO
 * @author: Tokyo_God
 * @date: 2020/12/28 15:40
 * Role of:
 * @version: v1.0
 */
@Component
public class MailService {
    @Autowired
    private JavaMailSender sender;
    @Value("${spring.mail.username}")
    private String from;

    public void sendHtmlMail(String to ,String subject,String content){
        MimeMessage mimeMessage=sender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setTo(to);//被发送的 人
            mimeMessageHelper.setFrom(from);//发送者
            mimeMessageHelper.setSubject(subject);//主题名称
            mimeMessageHelper.setText(content,true);//内容
            sender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
}
